package cn.chiship.framework.third.biz.wxmini.controller;

import cn.chiship.framework.common.annotation.LoginUser;
import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.framework.third.biz.all.service.MemberUserService;
import cn.chiship.framework.third.biz.wxmini.pojo.dto.AppletRegistrationDto;
import cn.chiship.framework.third.biz.wxmini.pojo.dto.DecryptingDto;
import cn.chiship.sdk.cache.vo.CacheUserVO;
import cn.chiship.sdk.core.annotation.Authorization;
import cn.chiship.sdk.core.annotation.NoParamsSign;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.ip.IpUtils;
import cn.chiship.sdk.third.wx.network.WxMiniProgramServicesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author lijian
 */
@RestController
@RequestMapping("/wxMini")
@Api(tags = "登录控制器")
public class WxMiniController {

    @Resource
    private MemberUserService memberUserService;

    @SystemOptionAnnotation(describe = "小程序Code换取Session", option = BusinessTypeEnum.SYSTEM_OPTION_OTHER)
    @ApiOperation(value = "小程序Code换取Session")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "小程序登录获得的Code", required = true, dataTypeClass = String.class, paramType = "query"),
    })
    @GetMapping("/code2Session")
    @NoParamsSign
    public ResponseEntity<BaseResult> code2Session(@RequestParam(value = "code") String code) {
        WxMiniProgramServicesUtil weiXinMiniProgramServicesUtil = WxMiniProgramServicesUtil.getInstance().config();
        BaseResult baseResult = weiXinMiniProgramServicesUtil.code2Session(code);
        return new ResponseEntity<>(baseResult, HttpStatus.OK);

    }

    @SystemOptionAnnotation(describe = "解密小程序敏感数据", option = BusinessTypeEnum.SYSTEM_OPTION_OTHER)
    @ApiOperation(value = "解密小程序敏感数据")
    @PostMapping("/decryptingOpenData")
    @NoParamsSign
    public ResponseEntity<BaseResult> decryptingOpenData(@RequestBody @Valid DecryptingDto decryptingDto) {
        WxMiniProgramServicesUtil weiXinMiniProgramServicesUtil = WxMiniProgramServicesUtil.getInstance().config();
        BaseResult baseResult = weiXinMiniProgramServicesUtil.decryptingOpenData(decryptingDto.getEncryptedData(), decryptingDto.getIv(), decryptingDto.getSessionKey());
        return new ResponseEntity<>(baseResult, HttpStatus.OK);
    }

    @SystemOptionAnnotation(describe = "小程序登录", option = BusinessTypeEnum.SYSTEM_OPTION_LOGIN)
    @ApiOperation(value = "小程序登录")
    @PostMapping("/appletLogin")
    @NoParamsSign
    public ResponseEntity<BaseResult> appletLogin(HttpServletRequest request, @RequestBody @Valid AppletRegistrationDto appletRegistrationDto) {
        BaseResult baseResult = memberUserService.appletLogin(appletRegistrationDto, IpUtils.getIpAddr(request));
        return new ResponseEntity<>(baseResult, HttpStatus.OK);
    }

    @GetMapping("/getInfo")
    @Authorization
    @NoParamsSign
    public ResponseEntity<BaseResult> getInfo(@LoginUser CacheUserVO userVO) {
        return new ResponseEntity<>(BaseResult.ok(userVO), HttpStatus.OK);
    }
}
